package com.yanqu.road.dao.impl.activity.goldautumn;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.goldautumn.GoldAutumnBuff;
import com.yanqu.road.entity.activity.goldautumn.GoldAutumnCell;
import com.yanqu.road.entity.activity.goldautumn.GoldAutumnUserData;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class GoldAutumnUserDataDaoImpl extends TempDataObjectDao<GoldAutumnUserData> {

    public static String getGoldAutumnUserDataSql = "select * from `t_u_activity_gold_autumn_user_data` where `activityId`=? and `userId`=?";
    public GoldAutumnUserData getGoldAutumnUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGoldAutumnUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGoldAutumnUserDataSql = "update `t_u_activity_gold_autumn_user_data` set score=?,curCellId=?,maxCellId=?,totalCost=?,stepSet=?,cellList=?,buffMap=?,lastCellList=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(GoldAutumnUserData goldAutumnUserData) {
        goldAutumnUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, goldAutumnUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, goldAutumnUserData.getCurCellId()));
        para.put(i++, new DbParameter(Types.INTEGER, goldAutumnUserData.getMaxCellId()));
        para.put(i++, new DbParameter(Types.INTEGER, goldAutumnUserData.getTotalCost()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goldAutumnUserData.getStepSet())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goldAutumnUserData.getCellList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goldAutumnUserData.getBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goldAutumnUserData.getLastCellList())));
        para.put(i++, new DbParameter(Types.INTEGER, goldAutumnUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goldAutumnUserData.getUserId()));
        if (getDb().executeNoneQuery(updateGoldAutumnUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGoldAutumnUserDataSql = "insert into `t_u_activity_gold_autumn_user_data`(activityId,userId,score,curCellId,maxCellId,totalCost,stepSet,cellList,buffMap,lastCellList) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(GoldAutumnUserData goldAutumnUserData) {
        goldAutumnUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, goldAutumnUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goldAutumnUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, goldAutumnUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, goldAutumnUserData.getCurCellId()));
        para.put(i++, new DbParameter(Types.INTEGER, goldAutumnUserData.getMaxCellId()));
        para.put(i++, new DbParameter(Types.INTEGER, goldAutumnUserData.getTotalCost()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goldAutumnUserData.getStepSet())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goldAutumnUserData.getCellList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goldAutumnUserData.getBuffMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(goldAutumnUserData.getLastCellList())));
        if (getDb().executeNoneQuery(insertGoldAutumnUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GoldAutumnUserData parseData(ResultSet rs) throws SQLException {
        GoldAutumnUserData goldAutumnUserData = new GoldAutumnUserData();
        goldAutumnUserData.setActivityId(rs.getInt("activityId"));
        goldAutumnUserData.setUserId(rs.getLong("userId"));
        goldAutumnUserData.setScore(rs.getLong("score"));
        goldAutumnUserData.setCurCellId(rs.getInt("curCellId"));
        goldAutumnUserData.setMaxCellId(rs.getInt("maxCellId"));
        goldAutumnUserData.setTotalCost(rs.getInt("totalCost"));
        goldAutumnUserData.setStepSet(JSON.parseObject(rs.getString("stepSet"), new TypeReference<ConcurrentHashSet<Integer>>(){}));
        goldAutumnUserData.setCellList(JSON.parseObject(rs.getString("cellList"), new TypeReference<List<GoldAutumnCell>>() {}));
        goldAutumnUserData.setLastCellList(JSON.parseObject(rs.getString("lastCellList"), new TypeReference<List<GoldAutumnCell>>() {}));
        goldAutumnUserData.setBuffMap(JSON.parseObject(rs.getString("buffMap"), new TypeReference<ConcurrentHashMap<Integer, GoldAutumnBuff>>() {}));

        return goldAutumnUserData;
    }
}
